package com.gimi.cloud.jimi.app.consumer.service.impl;

import com.gimi.cloud.jimi.app.consumer.feign.UserFeignService;
import com.gimi.cloud.jimi.app.consumer.service.UserService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.enums.ServiceNameConstants;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.*;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.sys.HtmlDTO;
import com.gimi.cloud.jimi.dto.user.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User service实现类
 * create by xiankejin on 2017-10-13
 */
@Service
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private UserFeignService userFeignService;

    @Autowired
    UserServiceImpl(UserFeignService userFeignService) {
        this.userFeignService = userFeignService;
    }

    @Override
    public void getUserRegisterCode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData, UserDTO.class);
        if (userDTO == null || userDTO.getMobilePhone() == null) {//验证参数合法性
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Pattern p = Pattern.compile(ServiceNameConstants.PHONE_FORMAT);
        Matcher m = p.matcher(userDTO.getMobilePhone());
        if (!m.matches()) {//验证手机号码格式
            logger.error("手机号格式错误");
            throw new MsgException(ResponseCodeEnum.ERR_PHONE_INVALID);
        }
        Util.sendResponse(userFeignService.getUserRegisterCode(userDTO), httpServletResponse);
    }

    @Override
    public void getModifypasswordCode(String requestData, HttpServletResponse response, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData, UserDTO.class);
        if (userDTO == null || userDTO.getMobilePhone() == null) {//验证参数合法性
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Pattern p = Pattern.compile(ServiceNameConstants.PHONE_FORMAT);
        Matcher m = p.matcher(userDTO.getMobilePhone());
        if (!m.matches()) {//验证手机号码格式
            logger.error("手机号格式错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(userFeignService.getModifypasswordCode(userDTO), response);
    }

    @Override
    public void getInformation(String requestData, HttpServletResponse response, HttpServletRequest request) {
        Map<String, String> map = JsonUtil.jsonToMap(requestData);
        String userId = request.getHeader("userId");
        if (StringUtil.isEmpty(userId) || "0".equals(userId)) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        map.put("userId", userId);
        map.put("ip", getRemortIP(request));
        Util.sendResponse(userFeignService.getInformation(map), response);
    }

    @Override
    public void getRegistAgreement(String requestData, HttpServletResponse response, HttpServletRequest request) {
        logger.info("getRegistAgreement:" + requestData);
        HtmlDTO htmlDTO = JsonUtil.jsonToBean(requestData, HtmlDTO.class);
        if(StringUtil.isBlank(htmlDTO.getType())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(userFeignService.getRegistAgreement(htmlDTO), response);
    }

    private String getRemortIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
		/*if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}*/
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if(ip != null && ip.length() > 0){
            String[] ipArray = ip.split(",");
            if (ipArray != null && ipArray.length > 1) {
                return ipArray[0];
            }
            return ip;
        }
        return "";
    }
}
